//
//  BVMineTimeAlertView.m
//  XiangYu
//
//  Created by YYKJ on 2017/6/23.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVMineTimeAlertView.h"

#define AlertW [UIScreen mainScreen].bounds.size.width-80

@interface BVMineTimeAlertView ()

@property(nonatomic ,retain) UIView *alertView;

@property (nonatomic ,strong) UIView           *upLine;
@property (nonatomic ,strong) UIButton         *upLeftBtn;
@property (nonatomic ,strong) UIButton         *upRightBtn;
@property (nonatomic ,strong) UIDatePicker     *datepicker;

@property (nonatomic ,strong) NSString         *tempStr;

@end

@implementation BVMineTimeAlertView

- (instancetype)init{
    
    if (self = [super init]) {
        
        self.frame = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:128/255.0 green:128/255.0 blue:128/255.0 alpha:0.5];
        self.alertView = [[UIView alloc]init];
        [self addSubview:self.alertView];
        [self.alertView setBackgroundColor:[UIColor whiteColor]];
        [self.alertView setFrame:CGRectMake(0, 0, AlertW, 200)];
        [self.alertView.layer setCornerRadius:5.0];
        [self.alertView.layer setPosition:self.center];
        
        self.upLeftBtn = [self upBtn];
        [self.alertView addSubview:self.upLeftBtn];
        [self.upLeftBtn setFrame:CGRectMake(10, 10, 50, 25)];
        [self.upLeftBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.upLeftBtn setTag:100];
        
        self.upRightBtn = [self upBtn];
        [self.alertView addSubview:self.upRightBtn];
        [self.upRightBtn setFrame:CGRectMake(AlertW-10-50, 10, 50, 25)];
        [self.upRightBtn setTitle:@"确认" forState:UIControlStateNormal];
        [self.upRightBtn setTag:200];
        
        self.upLine = [[UIView alloc]initWithFrame:CGRectMake(0,40,AlertW,1.0)];
        [self.upLine setBackgroundColor:[UIColor BVLineGrayColor]];
        [self.alertView addSubview:self.upLine];
        
        self.datepicker = [[UIDatePicker alloc]init];
        [self.datepicker setFrame:CGRectMake(0, 50, self.alertView.width, self.alertView.height-60)];
        [self.datepicker setDatePickerMode:UIDatePickerModeDate];
        [self.datepicker setMaximumDate:[NSDate date]];
        [self.datepicker setMinimumDate:[NSDate dateWithTimeIntervalSince1970:0]];
        [self.datepicker addTarget:self action:@selector(datePickerClick:) forControlEvents:UIControlEventValueChanged];
        [self.alertView addSubview:self.datepicker];
        
    }
    return self;
}

-(void)datePickerClick:(UIDatePicker *)datePicker {

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    //按照formatter这种时间格式将NSDate类型转换成NSString
    NSString *str = [formatter stringFromDate:datePicker.date];
    NSLog(@"%@",str);
    _tempStr = str;
}

#pragma mark - 弹出
-(void)showMKPAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
}

-(void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
        
    }];
    
}

-(void)disBtnClick:(UIButton *)btn{
    if (btn.tag == 100) {
        [self removeFromSuperview];
    }else{
        if (self.AlertResult) {
            self.AlertResult(_tempStr);
            [self removeFromSuperview];
        }
    }
}

-(UIButton *)upBtn{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(disBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn setTitleColor:[UIColor BVBaseOrangeColor]forState:UIControlStateNormal];
    [btn.layer setCornerRadius:3.0];
    [btn.layer setMasksToBounds:YES];
    [btn.layer setBorderWidth:1.0];
    [btn.layer setBorderColor:[UIColor BVLineGrayColor].CGColor];
    
    return btn;
}

@end
